package com.syt.service.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.syt.service.pojo.PhoneBase;
import com.syt.service.pojo.PhoneUser;
import com.syt.service.service.PhoneUserService;
import com.syt.service.util.LoginRequired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：syt
 * @date ：Created in 2019-10-11 14:24
 * @description：管理控制器，控制管理员网页
 */
@Controller
public class AdminController {

    @Autowired
    PhoneUserService userService;

    @GetMapping("admin/index")
    @LoginRequired
    public String index(Model model,
                        @RequestAttribute String userId,
                        @RequestParam(value = "page_id", defaultValue = "1", required = false) Integer pageId) {
        // 用户数据加入
        PhoneUser user = userService.findUserById(userId);
        user.setUserPassword("xxxxxxx");
        model.addAttribute("user", user);
        /**
         * 访问数据地址，来获取json数据，并且解析为basephone数组
         */
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://127.0.0.1:8001/basePhone/list/" + pageId;
        String baseList = restTemplate.getForObject(path, String.class);
        JSONObject json = JSONObject.parseObject(baseList);
        List<PhoneBase> phoneBases = JSON.parseArray(json.getString("list"), PhoneBase.class);
        // 基础数据加入
        model.addAttribute("phoneBases", phoneBases);
        // 第几页页数
        model.addAttribute("pageNum", json.getInteger("pageNum"));
        model.addAttribute("pages", json.getInteger("pages"));
        return "index";
    }

    @GetMapping("admin/base/edit")
    public String index() {
        return "edit";
    }

    @Value("${syt.upload}")
    private String path;

    /**
     * 文件上传的api
     * @param file
     * @return
     */
    @PostMapping("api/upload")
    @ResponseBody
    public Object upload(MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        Map src = new HashMap();
        try {
            String filepath = path + "/" + file.getOriginalFilename();
            jsonObject.put("filepath", filepath);
            File destFile = new File(filepath);
            if(!destFile.getParentFile().exists()){
                destFile.mkdirs();
            }
            file.transferTo(destFile);
            src.put("src", "http://127.0.0.1/" + file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("msg", "上传失败," + e.getMessage());
        }
        jsonObject.put("data", src);
        return jsonObject;
    }
}
